package com.wantong.jianpu.activitys;


import android.Manifest;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import com.gyf.immersionbar.ImmersionBar;
import com.wantong.jianpu.BR;
import com.wantong.jianpu.R;
import com.wantong.jianpu.base.BaseXActivity;
import com.wantong.jianpu.databinding.ActivityMainBinding;
import com.wantong.jianpu.event.FaultListEvent;
import com.wantong.jianpu.event.MessageEvent;
import com.wantong.jianpu.fragments.main.HomeFragment;
import com.wantong.jianpu.fragments.main.MessageFragment;
import com.wantong.jianpu.fragments.main.MineFragment;
import com.wantong.jianpu.utils.ActivityManager;
import com.wantong.jianpu.viewmodels.MainViewModel;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;
import me.goldze.mvvmhabit.views.bottombar.BottomBarTab;
import me.goldze.mvvmhabit.views.bottombar.BottomCheckBar;

/**
 * @author PACY
 * @date 2022/3/23
 * description: 入口activity
 */
public class MainActivity extends BaseXActivity<ActivityMainBinding, MainViewModel> {
    /**
     * 需要进行检测的权限数组
     */
    private String[] permissionsGroup = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
    };

    private List<Fragment> mFragments = new ArrayList<>();

    private long exitTime = -1L;

    private RxPermissions rxPermissions;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        EventBus.getDefault().register(this);
        ImmersionBar.with(MainActivity.this).fitsSystemWindows(true).statusBarColor(R.color.app_color).init();
        ActivityManager.getActivityManager().addActivity(this);
        initFragments();
        initBottomCheckBar();
        checkPermission();
        initTbsSetting();
        viewModel.msgCount.observe(this, integer -> {
            binding.bottomBar.getItem(1).setUnreadCount(integer);
        });
    }

    //初始化fragment
    private void initFragments() {
        mFragments.add(HomeFragment.newInstance("工作台"));
        mFragments.add(MessageFragment.newInstance("通知"));
        mFragments.add(MineFragment.newInstance("我的"));
    }


    /**
     * 检测权限
     */
    private void checkPermission() {
        rxPermissions = new RxPermissions(this);
        rxPermissions.request(permissionsGroup).subscribe(granted -> {
            if (granted) {
//                ToastUtils.show("权限获取成功");
            } else {
//                ToastUtils.show("权限获取失败");
            }
        });
    }

    /**
     * TBS优化
     */
    private void initTbsSetting() {
        HashMap map = new HashMap();
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
        QbSdk.initTbsSettings(map);
    }


    /**
     * 两套图片  icon切换
     */
    private void initBottomCheckBar() {
        binding.bottomBar.setFragments(mFragments)
                .init(getSupportFragmentManager(), R.id.fly)
                .addItem(new BottomBarTab(MainActivity.this, R.drawable.home_tab_bg, "工作台"))
                .addItem(new BottomBarTab(MainActivity.this, R.drawable.msg_tab_bg, "通知"))
                .addItem(new BottomBarTab(MainActivity.this, R.drawable.my_tab_bg, "我的"))
                .defaultIndex(0);
        binding.bottomBar.setOnTabSelectedListener(new BottomCheckBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                switch (position) {
                    case 0:
                    case 1:
                        ImmersionBar.with(MainActivity.this).fitsSystemWindows(true).statusBarColor(R.color.app_color).init();
                        break;
                    case 2:
                        ImmersionBar.with(MainActivity.this).fitsSystemWindows(false).statusBarColor(R.color.transparent).init();
                        break;
                }
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {
            }
        });

    }

    @Subscribe
    public void onEvent(MessageEvent event){
        viewModel.getMessage();
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - exitTime > 2000) {
            ToastUtils.show("再按一次返回退出");
            exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }
}